<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/myStrategyComment.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.js"></script>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/plugins/raty/jquery.raty.js"></script>

    <script>
        $(function () {
            //获取用户信息之后要转换成json数据
            var user = JSON.parse(sessionStorage.getItem("user"));
            var params = getParams();
            var star;//星级评价数
            var imgId;//图片上传的编号
            var tags = new Array();//所有的标签
            var imgUrls = "";//

            //初始化星级评价插件
            $(".star").raty({
                score: function () {
                    star = $(this).attr("data-num");
                },
                click: function (score, evt) {
                    star = score;
                },
                starOn: 'js/plugins/raty/img/star-on.png',
                starOff: 'js/plugins/raty/img/star-off.png',
                starHalf: 'js/plugins/raty/img/star-half.png',
                readOnly: false,
                halfShow: true,
                size: 34,
            })

            //点击上传图片
            $(".rect").click(function () {
                imgId = $(this).data("id");//点击要上传的图片编号
                //主要用作触发文件上传框的点击动作
                var picUrl = $(".rect[data-id='" + imgId + "'] .imgUrl").attr('src');
                if (picUrl) {//如果有图片则取消，删除图片
                    $(".rect[data-id='" + imgId + "']").html('<img class="imgUrl"/>上传');
                    $(".rect[data-id='" + imgId + "'] .imgUrl").attr('src', "");
                    return;
                }
                $("[name=pic]").click();
            })

            //图片上传
            $("[name=pic]").change(function () {
                if ($(this).val()) {
                    $("#imgForm").ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            //将上传的图片回显到页面中
                            $(".rect[data-id='" + imgId + "']").html('<img class="imgUrl"/>取消');
                            $(".rect[data-id='" + imgId + "'] .imgUrl").attr('src', data.url);
                            var urls = $(".rect[data-id='" + imgId + "'] .imgUrl").attr("src");
                        }
                    })
                }
            })

            //针对表单的属性配置
            var options = {
                type: "POST",
                url: "/strategies/" + params.id + "/comments/" + user.id,
                //在表单提交之前添加额外的参数,arr是要进行提交的参数的数组
                beforeSubmit: function (arr) {
                    arr.push({name: "myTags", value: $("[name=myTags]").val()});//自定义标签
                    arr.push({name: "comment", value: $("[name=comment]").val()});//评论内容
                    arr.push({name: "starNum", value: star});//星级评价
                    arr.push({name: "imgUrls", value: imgUrls});//上传图片url .attr("src")
                    arr.push({name: "tagIds", value: tags});//标签
                },
                //保存成功,跳转页面(我的游记页面)
                success: function (data) {
                    if (data.success) {
                        //弹出窗口提示评价成功
                        $(document).dialog({
                            type: 'confirm',
                            style: 'IOS',
                            titleshow: false,
                            content: '评价成功',
                            buttons: [
                                {
                                    name: '确定',
                                    callback: function () {
                                        window.location.href = "strategyCatalogs.html?strategyId=" + params.id;
                                    }
                                }
                            ]
                        });
                    } else {
                        $(document).dialog({
                            autoClose: 2500,
                            content: "评价失败，请再试一次！！"
                        })
                    }
                }
            };

            //将表单变为异步请求
            $(".comment").ajaxForm(options);
            $("#submitBtn").click(function () {
                //将上传的图片回显到页面中
                $.each($('input:checkbox:checked'), function () {
                    tags.push($(this).val());
                });
                $.each($(".rect .imgUrl"), function () {
                    var imgUrl = $(this).attr("src");
                    if (imgUrl != null) {
                        imgUrls += imgUrl + ";";
                    }
                });
                $(".comment").submit();
            })

        })
    </script>
</head>


<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:window.history.go(-1)">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>

        </div>
        <div class="col">
            <span>点评</span>
        </div>
        <div class="col"></div>
    </div>
</div>

<div class="comment">
    <div class="container star">
        <p>你对该旅游地的评价</p>
        <div id="star" data-num="3.5"></div>
    </div>
    <hr/>
    <div class="container tag">
        <p>请为该旅游地选择合适的标签（大家都在评）</p>
        <div class="btn-group-toggle" data-toggle="buttons">
            <label class="btn btn-outline-danger ">
                <input type="checkbox" autocomplete="off" value="1"> 价格便宜
            </label>
            <label class="btn btn-outline-danger">
                <input type="checkbox" autocomplete="off" value="2"> 住宿方便
            </label>
            <label class="btn btn-outline-danger ">
                <input type="checkbox" autocomplete="off" value="3"> 旅游景点多
            </label>
            <label class="btn btn-outline-danger ">
                <input type="checkbox" autocomplete="off" value="4">适合拍照
            </label>
            <label class="btn btn-outline-danger">
                <input type="checkbox" autocomplete="off" value="5">吃的很多
            </label>
            <label class="btn btn-outline-danger">
                <input type="checkbox" autocomplete="off" value="6">交通不方便
            </label>
        </div>

        <p>添加你自己的标签</p>
        <input name="myTags" class="form-control" placeholder="请使用空格隔开标签，每个标签不超8个字"/>

        <p>更多评价内容</p>
        <textarea name="comment" id="comment" class="form-control" rows="5" placeholder="请填写你对该旅游地的评价"></textarea>
    </div>
    <div class="container imgs ">
        <p>上传图片</p>


        <div class="row">
            <form action="/images" id="imgForm" method="post" enctype="multipart/form-data">
                <input type="file" style="display: none" name="pic"/>
            </form>
            <div class="col-3 rect" data-id="1">
                <img class="imgUrl"/>上传
            </div>
            <div class="col-3 rect" data-id="2">
                <img class="imgUrl"/>上传
            </div>
            <div class="col-3 rect" data-id="3">
                <img class="imgUrl"/>上传
            </div>
            <div class="col-3 rect" data-id="4">
                <img class="imgUrl"/>上传
            </div>
            <div class="col-3 rect" data-id="5">
                <img class="imgUrl"/>上传
            </div>
            <div class="col-3 rect" data-id="6">
                <img class="imgUrl"/>上传
            </div>
        </div>
    </div>

</div>


<div class="operation">
    <button class="btn" id="submitBtn">提交评价</button>
    <button class="btn" onclick="window.history.go(-1)">放弃评价</button>
</div>

</body>


</html>